<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <!-- 商品列表项区域 -->
      <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getGoodsSelected[item.id]"
              @change="changeGoodsSelected(item.id,$store.getters.getGoodsSelected[item.id])"
            ></mt-switch>
            <img :src="item.thumb_path" alt />
            <div class="info">
              <h1>{{item.title}}</h1>
              <p>
                <span class="price">￥{{item.sell_price}}</span>
                <!-- getGoodsCount返回的是一个对象 用[item.id]来获取键为item.id的值 -->
                <numbox
                  style="height:25px"
                  :initcount="$store.getters.getGoodsCount[item.id]"
                  :goodsid="item.id"
                  :max="$store.getters.getGoodsQuantity[item.id]"
                ></numbox>
                <!-- 怎么获取购物车中商品的数量（本地car中的count） -->
                <!-- 1.先创建一个空对象，然后循环购物车中所有商品的数据，把当前循环商品的
                id作为对象的属性名，count作为对象的属性值 
                最后得到的对象：{属性名1:count1，属性值2:count2}-->

                <!-- 此处的 i 用于删除 goodslist 中的数据
                item.id 用于删除 store里car 中的数据-->
                <a href="#" @click.prevent="remove(item.id,i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner compute">
          <div class="left">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span> 件,
              总价：
              <span
                class="red"
              >￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    <!-- <p>{{$store.getters.getGoodsSelected}}</p>
    <p>{{$store.getters.getGoodsSelected["88"]}}</p> -->
  </div>
</template>

<script>
import numbox from "../subcomponents/shopcar_numbox.vue";
export default {
  data() {
    return {
      goodslist: [] //购物车中所有商品列表
    };
  },
  created() {
    this.getGoodsList();
    
  },
  methods: {
    getGoodsList() {
      //获取 store 中所有商品的id 并拼接出一个以逗号分隔的字符串
      var idArr = [];
      this.$store.state.car.forEach(item => {
        idArr.push(item.id);
      });
      //   如果购物车中没有商品 直接返回
      if (idArr.length === 0) {
        return;
      }
      // 获取购物车商品列表
      this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then(result => {
          if (result.body.status === 0) {
            // console.log(result.body.message)
            this.goodslist = result.body.message;
          }
        });
      // console.log(this.$store.getters.getGoodsSelected)
    },
    remove(id, index) {
      // 根据 index 删除 goodslist 中的数据
      this.goodslist.splice(index, 1);
      // 根据 id 删除 store里car 中的数据
      this.$store.commit("removeFromCar", id);
    },
    changeGoodsSelected(id, val) {
      //将id为id的商品的 selected 改为 val
      // console.log(val);
      this.$store.commit("updateGoodsSelected", { id: id, selected: val });
    }
  },
  components: {
    numbox
  }
};
</script>

<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;
  .goods-list {
    .mui-card-content-inner {
      display: flex;
      //垂直居中
      justify-content: start;
      align-items: center;
      p {
        margin-bottom: 0;
      }
    }
    img {
      width: 50px;
      height: 50px;
      margin-left: 5px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin: 0 5px;
      h1 {
        font-size: 15px;
        margin-bottom: 10px;
      }
      .price {
        color: red;
        font-size: 16px;
        font-weight: bold;
      }
    }
  }
  .compute {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-size: 17px;
      font-weight: bold;
    }
  }
}
</style>